package com.zyf.controller;

import com.zyf.common.result.Result;
import com.zyf.model.cmn.Dict;
import com.zyf.service.DictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author zyf 数据字典管理 controller
 * @date 2021年04月30日 16:47
 */
@Api(tags = "数据字典接口")
@RestController
@RequestMapping("/admin/cmn/dict")
public class DictController {

    @Autowired
    private DictService dictService;

    @ApiOperation(value = "根据上级id获取子节点数据列表")
    @ApiImplicitParam(name = "id",value = "上级节点id",paramType = "path",required = true,dataType = "long")
    @GetMapping("/findChildData/{id}")
    public Result findChildData(@PathVariable Long id){
        List<Dict> list = dictService.findChildData(id);
        return Result.ok(list);
    }


    @ApiOperation(value = "导出数据字典到excel中")
    @GetMapping("/exportData")
    public void exportDict(HttpServletResponse response){
        dictService.exportDict(response);
    }

    @ApiOperation(value = "excel导入数据字典到数据库中")
    @PostMapping("/importData")
    public Result importData(MultipartFile file){
        dictService.importData(file);
        return Result.ok();
    }

    @GetMapping("/getName/{dictCode}/{value}")
    @ApiOperation(value = "根据dictcode和value查询name")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dictCode",value = "数据字典编码"),
            @ApiImplicitParam(name = "value",value = "数据字典的值")
    })
    public String getName(@PathVariable String dictCode,@PathVariable String value){
        String dictName = dictService.getDictName(dictCode,value);
        return dictName;
    }

    @GetMapping("/getName/{value}")
    @ApiOperation(value = "根据value查询name")
    @ApiImplicitParam(name = "value",value = "数据字典的value")
    public String getName(@PathVariable String value){
        String dictName = dictService.getDictName("",value);
        return dictName;
    }

    @ApiOperation(value = "根据dictCode查询下级节点")
    @GetMapping("/findByDictCode/{dictCode}")
    @ApiImplicitParam(name = "dictCode",value = "数据字典编码")
    public Result findByDictCode(@PathVariable String dictCode){
        List<Dict> dictList = dictService.findByDictCode(dictCode);
        return Result.ok(dictList);
    }
}
